function ajaxnewmsgpage(url, id, sid, delay){
    var page_request = false;
    if (window.XMLHttpRequest) { // if Mozilla, Safari etc
	page_request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){ // if IE
	try {
	    page_request = new ActiveXObject("Msxml2.XMLHTTP");
	} 	
	catch (e){
	    try{
		page_request = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e){}
	}
    }
    else
        return false;
    page_request.onreadystatechange=function(){
	loadnewmsgpage(page_request, id, sid, delay);
    }
    page_request.open('GET', url, true);
    page_request.send(null);
}

function loadnewmsgpage(page_request, id, sid, delay){
    try {
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
	    if (page_request.responseText.length) {
		
		var lend = page_request.responseText.indexOf('(id)\n');
		if (lend!=-1) {
		    id = page_request.responseText.substring(0,lend);
		    lend += 5;
		}
		else 
		    lend = 0;
		var html = page_request.responseText.substring(lend);
		
		lend = html.indexOf('(ircandjabberusers)\n');
		if (lend!=-1) {
		    var ircandjabberusers = html.substring(0,lend);
		    lend += 20;
		    var users = document.getElementById('users');
		    users.innerHTML = ircandjabberusers;
		}
		else 
		    lend = 0;
		html = html.substring(lend);
		
		var html = page_request.responseText.substring(lend);
		var table = document.getElementById('msgstbl');
		var rows = table.getElementsByTagName('tr');
		var n = 0;
		var new_rows = html.split("\n");
		for (i in new_rows) {
		    if (new_rows[i].length) {
			var new_cols = new_rows[i].split("</td>");
			var r;
			var r_created = 0;
			for (j in new_cols) {
			    var tdidx1 = new_cols[j].indexOf("<td");
			    var tdidx2 = new_cols[j].indexOf(">");
			    if (tdidx1>=0 && tdidx2>=0) {
				var col = new_cols[j].substring(tdidx2+1);
				if (!r_created) {
				    r_created = 1;
				    r = table.insertRow(-1);
				    n++;
				}
				var c = r.insertCell(j);
				c.innerHTML = col;
				var opts = new_cols[j].substring(tdidx1+3, tdidx2);
				if (opts.length) {
				    if (opts.indexOf("colspan='2'") >= 0)
					c.colSpan = 2;
				    var cidx = opts.indexOf("class='");
				    if (cidx >= 0) {
					var cidx2 = opts.indexOf("'", cidx+7);
					if (cidx2 >= 0) {
					    var cssclass = opts.substring(cidx+7, cidx2);
					    c.className = cssclass;
					}
				    }
				}
			    }
			}
		    }
		}
		if (n) {
		    var title = document.title;
		    if (html.length && title.substr(0,1) == "(") {
			var idx = title.indexOf(')');
			if (idx!=-1) {
			    var tn = title.substring(1,idx);
			    title = title.substring(idx+1);
			    n += parseInt(tn);
			}
		    }
		    document.title = "("+n+")"+title;
		}
	    }
	    setTimeout("check_new("+id+",'"+sid+"',"+delay+")",delay);
	}
    }
    catch (e){
    }
}

function check_new(id, sid, delay){
    ajaxnewmsgpage("/wubchain/in?ID="+id+"&IN=1&SID="+sid, id, sid, delay);
}

function insert_nick(nick) {
    document.getElementById('cmsg').value = "/msg " + nick + " " + document.getElementById('cmsg').value;
}
